﻿Imports System.IO
Imports System.Xml.Serialization
Public Class CLDS_Preferences_Serial
#Region "Private declarations"
    ' Mirror variables in CLDS_ACAD
    Private d As New CLDS_Preferences
    Public v_b_orgn As String
    Public v_b_dvce As String
    Public v_t_dvce As String
    Public v_b_node As String
    Public v_b_nodm As String
    Public v_t_node As String
    Public v_t_full As String
    Public v_t_type As String
    Public v_t_area As String
    Public v_t_seg As String
    Public v_t_comm As String
    Public v_g_clds As String
    Public v_l_dvce As String
    Public v_l_node As String
    Public v_l_nodm As String
    Public v_l_sgmt As String
    Public v_l_used As String
    Public v_l_appd As String
    Public v_l_hide As String
    Public v_l_jump As String
    Public v_l_warn As String
    Public v_l_eror As String
    Public v_l_crte As String
    Public v_l_arte As String
    Public v_l_isrt As String
    Public v_l_updt As String
    Public v_l_dele As String
    Public v_l_offr As String
    Public v_l_hlit As String
    Public v_l_brek As String
    Public v_x_prec As String
    Public v_x_fact As String
    Public v_c_dvce As String
    Public v_c_node As String
    Public v_c_nodm As String
    Public v_c_sgmt As String
    Public v_c_used As String
    Public v_c_appd As String
    Public v_c_hide As String
    Public v_c_jump As String
    Public v_c_warn As String
    Public v_c_eror As String
    Public v_c_crte As String
    Public v_c_arte As String
    Public v_c_isrt As String
    Public v_c_updt As String
    Public v_c_dele As String
    Public v_c_offr As String
    Public v_c_brek As String
    Public v_h_fdev As String
    Public v_h_tdev As String
    Public v_h_rnod As String
    Public v_h_pnod As String
    Public v_n_jump As String
    Public v_n_offr As String
    Public v_f_dvce As Integer
    Public v_a_dvce As Integer
    Public v_f_node As Integer
    Public v_f_nodm As Integer
    Public v_a_node As Integer
    Public v_a_nodm As Integer
    Public v_r_node As String
    Public v_r_pseg As String
    Public v_x_psln As String
    Public v_x_pfil As String
    Public v_x_timo As Integer
    Public v_x_maxe As Integer
    Public v_x_rfsh As Integer
    Public v_x_lint As Integer
    Public v_x_dlpl As String
    Public v_r_xmlf As String
    Public v_r_xmld As String
    Public v_c_seqn As String
#End Region
#Region "Formatting functions"
    Private Function C2S(ByVal c As Color) As String
        Dim s As String
        s = Trim(Str$(c.R)) & "," & Trim(Str$(c.G)) & "," & Trim(Str$(c.B))
        Return s
    End Function
    Private Function L2S(ByVal l As CLDS_Colour_Collection) As String
        Return l.ToString()
    End Function
    Private Function SC(ByVal s As String, ByVal v As Color) As Color
        Dim x As String = s
        If x Is Nothing OrElse x = "" Then
            Return v
        End If
        Dim c As Color
        Dim r As Integer = 0, g As Integer = 0, b As Integer = 0
        Dim a As Object = Split(x, ",")
        r = Val(a(0)) : g = Val(a(1)) : b = Val(a(2))
        c = Color.FromArgb(r, g, b)
        Return c
    End Function
    Private Function SL(ByVal s As String, ByVal v As CLDS_Colour_Collection) As CLDS_Colour_Collection
        Dim x As String = s
        If x Is Nothing OrElse x = "" Then
            Return v
        End If
        v.FromString(s)
        Return v
    End Function
    Private Function SS(ByVal s As String, ByVal v As String) As String
        Dim x As String = s
        If x Is Nothing OrElse x = "" Then
            x = v
        End If
        Return x
    End Function
    Private Function SD(ByVal s As String, ByVal v As Double) As Double
        Dim x As Double = s
        If s Is Nothing OrElse s = "" Then
            x = v
        End If
        Return x
    End Function
    Private Function SI(ByVal s As String, ByVal v As Integer) As Integer
        Dim x As Integer = s
        If s Is Nothing OrElse s = "" Then
            x = v
        End If
        Return x
    End Function
    Private Function SB(ByVal s As String, ByVal v As Boolean) As Boolean
        Dim x As Boolean
        If s Is Nothing OrElse s = "" Then
            x = v
        ElseIf UCase(s) = "TRUE" Then
            x = True
        Else
            x = False
        End If
        Return x
    End Function
#End Region
#Region "Save and load methods"
    Public Sub SavePreferences(ByVal p_prefs As CLDS_Preferences, Optional ByVal p_path As String = "")
        Dim v_path As String
        v_path = p_path
        If v_path = "" Then
            v_path = p_prefs.g_file
        End If
        If v_path = "" Then
            v_path = Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name) & "\CMPIC_Prefs.xml"
        End If
        Me.v_b_orgn = p_prefs.b_orgn
        Me.v_b_dvce = p_prefs.b_dvce
        Me.v_t_dvce = p_prefs.t_dvce
        Me.v_b_node = p_prefs.b_node
        Me.v_b_nodm = p_prefs.b_nodm
        Me.v_t_node = p_prefs.t_node
        Me.v_t_type = p_prefs.t_type
        Me.v_t_area = p_prefs.t_area
        Me.v_t_seg = p_prefs.t_seg
        Me.v_t_comm = p_prefs.t_comm
        Me.v_g_clds = p_prefs.g_clds
        Me.v_l_dvce = p_prefs.l_dvce
        Me.v_l_node = p_prefs.l_node
        Me.v_l_nodm = p_prefs.l_nodm
        Me.v_l_sgmt = p_prefs.l_sgmt
        Me.v_l_used = p_prefs.l_used
        Me.v_l_appd = p_prefs.l_appd
        Me.v_l_hide = p_prefs.l_hide
        Me.v_l_jump = p_prefs.l_jump
        Me.v_l_warn = p_prefs.l_warn
        Me.v_l_eror = p_prefs.l_eror
        Me.v_l_crte = p_prefs.l_crte
        Me.v_l_arte = p_prefs.l_arte
        Me.v_l_isrt = p_prefs.l_isrt
        Me.v_l_updt = p_prefs.l_updt
        Me.v_l_dele = p_prefs.l_dele
        Me.v_l_offr = p_prefs.l_offr
        Me.v_l_hlit = p_prefs.l_hlit
        Me.v_l_brek = p_prefs.l_brek
        Me.v_x_prec = p_prefs.x_prec
        Me.v_x_fact = p_prefs.x_fact
        Me.v_c_dvce = C2S(p_prefs.c_dvce)
        Me.v_c_node = C2S(p_prefs.c_node)
        Me.v_c_nodm = C2S(p_prefs.c_nodm)
        Me.v_c_sgmt = C2S(p_prefs.c_sgmt)
        Me.v_c_used = C2S(p_prefs.c_used)
        Me.v_c_appd = C2S(p_prefs.c_appd)
        Me.v_c_hide = C2S(p_prefs.c_hide)
        Me.v_c_jump = C2S(p_prefs.c_jump)
        Me.v_c_warn = C2S(p_prefs.c_warn)
        Me.v_c_eror = C2S(p_prefs.c_eror)
        Me.v_c_crte = C2S(p_prefs.c_crte)
        Me.v_c_arte = C2S(p_prefs.c_arte)
        Me.v_c_isrt = C2S(p_prefs.c_isrt)
        Me.v_c_updt = C2S(p_prefs.c_updt)
        Me.v_c_dele = C2S(p_prefs.c_dele)
        Me.v_c_offr = C2S(p_prefs.c_offr)
        Me.v_c_brek = C2S(p_prefs.c_brek)
        Me.v_h_fdev = C2S(p_prefs.h_fdev)
        Me.v_h_tdev = C2S(p_prefs.h_tdev)
        Me.v_h_rnod = C2S(p_prefs.h_rnod)
        Me.v_h_pnod = C2S(p_prefs.h_pnod)
        Me.v_n_jump = p_prefs.n_jump
        Me.v_n_offr = p_prefs.n_offr
        Me.v_f_dvce = p_prefs.f_dvce
        Me.v_f_node = p_prefs.f_node
        Me.v_f_nodm = p_prefs.f_nodm
        Me.v_a_dvce = p_prefs.a_dvce
        Me.v_a_node = p_prefs.a_node
        Me.v_a_nodm = p_prefs.a_nodm
        Me.v_r_node = p_prefs.r_node
        Me.v_r_pseg = p_prefs.r_pseg
        Me.v_x_psln = p_prefs.x_psln
        Me.v_x_pfil = p_prefs.x_pfil
        Me.v_x_timo = p_prefs.x_timo
        Me.v_x_maxe = p_prefs.x_maxe
        Me.v_x_rfsh = p_prefs.x_rfsh
        Me.v_x_lint = p_prefs.x_lint
        Me.v_x_dlpl = p_prefs.x_dlpl
        Me.v_r_xmlf = p_prefs.r_xmlf
        Me.v_r_xmld = p_prefs.r_xmld
        Me.v_c_seqn = L2S(p_prefs.c_seqn)
        If File.Exists(v_path) Then
            Dim d As String = "yyyyMMdd_HHmmss"
            Dim n As DateTime = DateTime.Now
            d = n.ToString(d)
            Dim b As String = v_path
            b = Path.GetFileNameWithoutExtension(b)
            b = b & "_" & d & ".xml"
            My.Computer.FileSystem.RenameFile(v_path, b)
            System.Diagnostics.Trace.TraceWarning("Renaming existing preferences file to " & b)
            System.Diagnostics.Trace.Flush()
        End If
        Dim objSerial As XmlSerializer
        Dim objStream As FileStream
        objStream = New FileStream(v_path, FileMode.OpenOrCreate, FileAccess.Write)
        objSerial = New XmlSerializer(Me.GetType)
        objSerial.Serialize(objStream, Me)
        objStream.Close()
        System.Diagnostics.Trace.TraceWarning("Preferences saved to file " & v_path)
        System.Diagnostics.Trace.Flush()
    End Sub
    Public Sub LoadPreferences(ByVal p_prefs As CLDS_Preferences, Optional ByVal p_path As String = "")
        Dim v_path As String
        v_path = p_path
        If v_path = "" Then
            v_path = CLDS_Static_Utils.CLDS_FindFile("CMPIC_Prefs.xml")
        End If
        Dim objSerial As XmlSerializer
        Dim objStream As FileStream
        Dim x As Boolean = Microsoft.VisualBasic.FileIO.FileSystem.FileExists(v_path)
        If Not x Then
            System.Diagnostics.Trace.TraceWarning("Unable to load preferences from XML file " & v_path)
            System.Diagnostics.Trace.Flush()
            MsgBox("Unable to load preferences from XML file" & vbLf & v_path, MsgBoxStyle.Critical, "CMPIC")
            Exit Sub
        End If
        System.Diagnostics.Trace.TraceWarning("Loading preferences from XML file " & v_path)
        System.Diagnostics.Trace.Flush()
        p_prefs.g_file = v_path
        objStream = New FileStream(v_path, FileMode.Open, FileAccess.Read)
        objSerial = New XmlSerializer(Me.GetType)
        Dim p_loaded As Object = objSerial.Deserialize(objStream)
        objStream.Close()
        p_prefs.b_orgn = SS(p_loaded.v_b_orgn, d.b_orgn)
        p_prefs.b_dvce = SS(p_loaded.v_b_dvce, d.b_dvce)
        p_prefs.t_dvce = SS(p_loaded.v_t_dvce, d.t_dvce)
        p_prefs.b_node = SS(p_loaded.v_b_node, d.b_node)
        p_prefs.b_nodm = SS(p_loaded.v_b_nodm, p_prefs.b_node)
        p_prefs.t_node = SS(p_loaded.v_t_node, d.t_node)
        p_prefs.t_type = SS(p_loaded.v_t_type, d.t_type)
        p_prefs.t_area = SS(p_loaded.v_t_area, d.t_area)
        p_prefs.t_seg = SS(p_loaded.v_t_seg, d.t_seg)
        p_prefs.t_comm = SS(p_loaded.v_t_comm, d.t_comm)
        p_prefs.g_clds = SS(p_loaded.v_g_clds, d.g_clds)
        p_prefs.l_dvce = SS(p_loaded.v_l_dvce, d.l_dvce)
        p_prefs.l_node = SS(p_loaded.v_l_node, d.l_node)
        p_prefs.l_nodm = SS(p_loaded.v_l_nodm, p_prefs.l_node)
        p_prefs.l_sgmt = SS(p_loaded.v_l_sgmt, d.l_sgmt)
        p_prefs.l_used = SS(p_loaded.v_l_used, d.l_used)
        p_prefs.l_appd = SS(p_loaded.v_l_appd, d.l_appd)
        p_prefs.l_hide = SS(p_loaded.v_l_hide, d.l_hide)
        p_prefs.l_jump = SS(p_loaded.v_l_jump, d.l_jump)
        p_prefs.l_warn = SS(p_loaded.v_l_warn, d.l_warn)
        p_prefs.l_eror = SS(p_loaded.v_l_eror, d.l_eror)
        p_prefs.l_crte = SS(p_loaded.v_l_crte, d.l_crte)
        p_prefs.l_arte = SS(p_loaded.v_l_arte, d.l_arte)
        p_prefs.l_isrt = SS(p_loaded.v_l_isrt, d.l_isrt)
        p_prefs.l_updt = SS(p_loaded.v_l_updt, d.l_updt)
        p_prefs.l_dele = SS(p_loaded.v_l_dele, d.l_dele)
        p_prefs.l_offr = SS(p_loaded.v_l_offr, d.l_offr)
        p_prefs.l_hlit = SS(p_loaded.v_l_hlit, d.l_hlit)
        p_prefs.l_brek = SS(p_loaded.v_l_brek, d.l_brek)
        p_prefs.x_prec = SD(p_loaded.v_x_prec, d.x_prec)
        p_prefs.x_fact = SD(p_loaded.v_x_fact, d.x_fact)
        p_prefs.c_dvce = SC(p_loaded.v_c_dvce, d.c_dvce)
        p_prefs.c_node = SC(p_loaded.v_c_node, d.c_node)
        p_prefs.c_nodm = SC(p_loaded.v_c_nodm, p_prefs.c_node)
        p_prefs.c_sgmt = SC(p_loaded.v_c_sgmt, d.c_sgmt)
        p_prefs.c_used = SC(p_loaded.v_c_used, d.c_used)
        p_prefs.c_appd = SC(p_loaded.v_c_appd, d.c_appd)
        p_prefs.c_hide = SC(p_loaded.v_c_hide, d.c_hide)
        p_prefs.c_jump = SC(p_loaded.v_c_jump, d.c_jump)
        p_prefs.c_warn = SC(p_loaded.v_c_warn, d.c_warn)
        p_prefs.c_eror = SC(p_loaded.v_c_eror, d.c_eror)
        p_prefs.c_crte = SC(p_loaded.v_c_crte, d.c_crte)
        p_prefs.c_arte = SC(p_loaded.v_c_arte, d.c_arte)
        p_prefs.c_isrt = SC(p_loaded.v_c_isrt, d.c_isrt)
        p_prefs.c_updt = SC(p_loaded.v_c_updt, d.c_updt)
        p_prefs.c_dele = SC(p_loaded.v_c_dele, d.c_dele)
        p_prefs.c_offr = SC(p_loaded.v_c_offr, d.c_offr)
        p_prefs.c_brek = SC(p_loaded.v_c_brek, d.c_brek)
        p_prefs.h_fdev = SC(p_loaded.v_h_fdev, d.h_fdev)
        p_prefs.h_tdev = SC(p_loaded.v_h_tdev, d.h_tdev)
        p_prefs.h_rnod = SC(p_loaded.v_h_rnod, d.h_rnod)
        p_prefs.h_pnod = SC(p_loaded.v_h_pnod, d.h_pnod)
        p_prefs.n_jump = SS(p_loaded.v_n_jump, d.n_jump)
        p_prefs.n_offr = SS(p_loaded.v_n_offr, d.n_offr)
        p_prefs.f_dvce = SI(p_loaded.v_f_dvce, d.f_dvce)
        p_prefs.f_node = SI(p_loaded.v_f_node, d.f_node)
        p_prefs.f_nodm = SI(p_loaded.v_f_nodm, p_prefs.f_node)
        p_prefs.a_dvce = SI(p_loaded.v_a_dvce, d.a_dvce)
        p_prefs.a_node = SI(p_loaded.v_a_node, d.a_node)
        p_prefs.a_nodm = SI(p_loaded.v_a_nodm, p_prefs.a_node)
        p_prefs.r_node = SS(p_loaded.v_r_node, d.r_node)
        p_prefs.r_pseg = SS(p_loaded.v_r_pseg, d.r_pseg)
        p_prefs.x_psln = SD(p_loaded.v_x_psln, d.x_psln)
        p_prefs.x_pfil = SB(p_loaded.v_x_pfil, d.x_pfil)
        p_prefs.x_timo = SI(p_loaded.v_x_timo, d.x_timo)
        p_prefs.x_maxe = SI(p_loaded.v_x_maxe, d.x_maxe)
        p_prefs.x_rfsh = SI(p_loaded.v_x_rfsh, d.x_rfsh)
        p_prefs.x_lint = SI(p_loaded.v_x_lint, d.x_lint)
        p_prefs.x_dlpl = SB(p_loaded.v_x_dlpl, d.x_dlpl)
        p_prefs.r_xmlf = SS(p_loaded.v_r_xmlf, d.r_xmlf)
        p_prefs.r_xmld = SS(p_loaded.v_r_xmld, d.r_xmld)
        p_prefs.c_seqn = SL(p_loaded.v_c_seqn, d.c_seqn)
    End Sub
#End Region
End Class
